<template>
  <el-container>
    <el-header style="height:80px;">
      <el-form ref="form" label-width="100px" style="margin-top:20px;">
        <el-row>
          <el-col :span="6">
            <el-form-item label="检查内容名称：">
              <el-input v-model="search.contentName"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-button class="querybtn" type="primary" @click="query(search)" style="color: #606266;background-color: #fff;border-color: #dcdfe6;margin-left:10px;">查询</el-button>
            <el-button type="danger" @click="handleAdd">新增</el-button>
            <el-dialog :title="title" :visible.sync="dialogFormVisible">
              <el-form label-width="100px" ref="content" :model="content" class="demo-ruleForm">
                <el-row>
                  <el-col :span="12">
                    <el-form-item label="检查内容名称：" prop="contentName">
                      <el-input v-model="content.contentName" auto-complete="off" maxlength="30"></el-input>
                    </el-form-item>
                  </el-col>
                  <el-col :span="12">
                    <el-form-item label="级别：">
                      <el-select v-model="content.examinationContent" onselect="true" placeholder="请选择状态">
                        <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value">
                        </el-option>
                      </el-select>
                    </el-form-item>
                  </el-col>
                </el-row>
              </el-form>
              <div slot="footer" class="dialog-footer" style="text-align: center">
                <el-button @click="dialogFormVisible = false">取 消</el-button>
                <el-button type="danger" @click="save()">保 存</el-button>`
              </div>
            </el-dialog>
          </el-col>
        </el-row>
      </el-form>
    </el-header>
    <el-main style="height:800px;">
      <section class="contant_head">
        <span>图表
          <!-- <sub>Form</sub> -->
        </span>
      </section>
      <el-table :data="tableData" border style="width: 98%;margin:10px;text-align:center;" :header-cell-style="getRowClass">
        <el-table-column prop="index" label="编号" width="120" height="200px"> </el-table-column>
        <el-table-column prop="contentName" label="检查内容名称"></el-table-column>
        <el-table-column prop="examinationContent" label="级别"></el-table-column>
        <el-table-column fixed="right" label="操作" width="210">
          <template slot-scope="scope">
            <!-- <el-button size="small" :id="gitButtonId(scope.$index+1)" class="edit-btn" @click="handleStop($event,scope.$index+1, scope.row)">
                <label v-if="scope.row.state==2">禁用</label>
                <label v-else>启用</label>
              </el-button> -->
            <el-button class="edit-btn" @click="handleEdit(scope.$index, scope.row)">
              <i></i>修改
            </el-button>
            <el-button class="edit-btn" @click="handleDelete(scope.$index, scope.row)">
              <i></i>删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination">
        <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="currentPage" :page-sizes="[10]" :page-size="limit" layout="total, sizes, prev, pager, next" :total="total" style="float:right">
        </el-pagination>
      </div>
    </el-main>
  </el-container>
</template>

<script>
import { api } from "@/fetch/api";


export default {
  name: "role",
  data() {
    return {
      options: [
        {
          value: "一般检查内容",
          label: "一般检查内容"
        },
        {
          value: "普通检查内容",
          label: "普通检查内容"
        },
        {
          value: "重要检查内容",
          label: "重要检查内容"
        }
      ],
      startDate: null,
      sub: [],
      endDate: null,
      department: api.LocalStorageService.get("role"),
      //修改 jlq-2018-2-6
      permissions: [],
      tableData: [],
      editNum: null,
      dialogVisible: false,
      dialogTableVisible: false,
      dialogFormVisible: false,
      treeDetail: [],
      treePermissionId: [],
      currentPage: 1, //當前頁碼
      limit: 10,
      total: null,
      edit: false,
      search: {
        contentId: 0,
        contentName: "",
        examinationContent: "",
        state: 0
      },
      content: {
        contentId: "",
        contentName: "",
        examinationContent: "",
        state: 0
      },
      params: [],
      title: "111"
    };
  },
  methods: {
    handleAdd() {
      // 新增角色
      this.dialogFormVisible = !this.dialogFormVisible;
      this.content = {};
      this.title = "添加";
      this.$nextTick(() => {
        this.$refs["form"].clearValidate();
      });
    },
    gitButtonId(index) {
      return index;
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      this.getUser(this.limit, val);
    },
    handleSizeChange(val) {
      this.limit = val;
      this.getUser(val, this.currentPage);
    },
    handleDelete(index, row) {
      this.$confirm("是否继续删除", {
        confirmButtonText: "确认",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          api
            .contentdelete({
              contentId: row.contentId
            })
            .then(response => {
              if (response.status === 100) {
                this.$message.success(response.message);
                this.getUser(this.limit, this.currentPage);
              } else {
              }
            });
        })
        .catch(() => { });
    },
    handleEdit(index, row) {
      this.content = row;
      this.editNum = index;
      this.dialogFormVisible = !this.dialogFormVisible;
      this.title = "修改";
      this.$nextTick(() => {
        this.$refs["form"].clearValidate();
      });
    },
    curry(e, node) {
      if (e.target.checked) {
        node.subDetail.forEach((sub, i) => {
          node.subDetail[i].checked = true;
        });
      } else {
        node.subDetail.forEach((sub, i) => {
          node.subDetail[i].checked = false;
        });
      }
    },
    getPermissionDetail() {
      api
        .PermissionPage({}, { limit: 0, pageIndex: 0 })
        .then(response => {
          if (response.status === 100) {
            this.permissions = response.result;
            this.myrole.rolepermissionlist = response.result;
            api.LocalStorageService.update("permissions", response.result);
          } else {
            this.$message.error(response.message);
          }
        })
        .catch(error => {
          this.$message.error(error);
        });
    },
    getUser(limit, pageIndex) {
      api
        .contentpage(this.search, { limit: limit, pageIndex: pageIndex })
        .then(response => {
          if (response.status === 100) {
            this.tableData = response.result.list;
            this.tableData.forEach(function (item, index) {
              item.index = index + 1 + (pageIndex - 1) * limit;
            });
            this.total = response.result.total;
          } else {
            this.$message.error(response.message);
          }
        });
    },
    getDepattment() {
      api
        .contentpage({}, { limit: 0, pageIndex: 0 })
        .then(response => {
          if (response.status === 100) {
            console.log(response);
            // let data = [
            //   {
            //     cRoleName: "",
            //     cRoleId: ""
            //   }
            // ];
            this.department = response.result.list;
            console.log(this.role);
            api.LocalStorageService.update("department", response.result.list);
          } else {
            this.$message.error(response.message);
          }
        })
        .catch(error => {
          this.$message.error(error);
        });
    },
    getRowClass({ row, column, rowIndex, columnIndex }) {
      if (rowIndex == 0) {
        return "background:rgba(242,242,242,1)";
      } else {
        return "";
      }
    },
    query(search) {
      api.contentpage(search, { limit: 10, pageIndex: 1 }).then(response => {
        if (response.status === 100) {
          this.tableData = response.result.list;
          this.currentPage = 1;
          this.tableData.forEach(function (item, index) {
            item.index = index + 1;
          });
          this.total = response.result.total;
        } else {
          this.$message.error(response.message);
        }
      });
    },
    save() {
      if (this.title === "添加") {
        if (!this.content.contentName) {
          this.$message.error("检查内容不能为空！！！");
        } else {
          api.contentadd(this.content).then(response => {
            console.log(this.content);
            if (response.status === 100) {
              this.$message.success("添加成功");
              this.dialogFormVisible = !this.dialogFormVisible;
              this.getUser(10, this.currentPage);
            } else {
              this.$message.error("添加错误");
            }
          });
        }
      } else {
        if (!this.content.contentName) {
          this.$message.error("检查内容不能为空！！！");
        } else {
          api.contentmodify(this.content).then(response => {
            if (response.status === 100) {
              this.$message.success("修改成功");
              this.dialogFormVisible = !this.dialogFormVisible;
              this.getUser(10, this.currentPage);
            } else {
              this.$message.error("修改错误");
            }
          });
        }
      }
    }
  },
  mounted() {
    //修改-2018-2-6
    // this.getPermission();
    this.getDepattment();
    this.getPermissionDetail();
    this.getUser(10, this.currentPage);
  }
};
</script>

<style lang="less" scoped>
@import url('../../assets/css/index.css');
.el-header {
  width: 98%;
  background: #fff;
  margin: 0 auto;
  margin-top: 20px;
  margin-bottom: 20px;
  .el-input,
  .el-select,
  .el-date-picker {
    width: 100%;
  }
}
.el-main {
  width: 98%;
  background: #fff;
  margin: 0 auto;
  padding: 0px;
  .contant_head {
    border-left: 3px solid #f7585d;
    background: #f9f9f9;
    height: 40px;
    span {
      line-height: 40px;
      font-size: 20px;
      padding-left: 10px;
      sub {
        font-size: 12px;
      }
    }
  }
}
</style>
<style>
.cell {
  text-align: center;
}
</style>
<style lang="stylus" module="$">
.role_setting_panel {
  :global {
    .el-form-item {
      display: block;
    }
  }
}

.role_warpper {
  :global {
    .el-dialog__header {
      background: rgba(242, 242, 242, 1);
      padding-bottom: 16px;
    }

    .el-dialog__title {
      font-weight: bold;
    }

    .el-form-item__label {
      float: none;
      display: inline-block;
      min-width: 120px;
    }

    .el-checkbox__input.is-checked .el-checkbox__inner, .el-checkbox__input.is-indeterminate .el-checkbox__inner {
      background-color: #F7585D;
      border-color: #F7585D;
    }

    .el-checkbox__input.is-checked+.el-checkbox__label {
      color: #F7585D;
    }

    .el-button--primary {
      color: #fff;
      background-color: #F7585D;
      border-color: #F7585D;
    }
  }
}
</style>




